/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.BoundedRangeModel;
import org.aswing.Component;
import org.aswing.DefaultIntegerBoundedRangeModel;
import org.aswing.JSlider;

import fever.utils.Stringifier;

import fvaswing.components.input.FvInputComponent;

/**
 * Use a <strong>JSlider</strong> as input component.
 * 
 * <p>Example using classic instanciation
 * {@code 
 *   var input : FvSliderInput = new FvSliderInput( true ); // rounded bounded range enabled.
 *   input.setMinimum( 1900 );
 *   input.setMaximum( 2100 );
 *   input.setValue( ( new Date() ).getFullYear() );
 *   
 *   FvInputComponentDialog.show( input, "Choose your year", this, _onInputResult );
 * }
 *  
 * <p>Example using static {@link #create()} instanciation
 * {@code 
 *   var slider : JSlider = new JSlider( JSlider.HORIZONTAL );
 *   slider.setMajorTickSpacing( 20 );
 *   slider.setMinorTickSpacing( 1 );
 *   
 *   var input : FvSliderInput = FvSliderInput.create( slider );
 *   input.setMinimum( 1900 );
 *   input.setMaximum( 2100 );
 *   input.setValue( ( new Date() ).getFullYear() );
 *   
 *   FvInputComponentDialog.show( input, "Choose your year", this, _onInputResult );
 * }
 *  
 * @see fvaswing.components.FvInputComponentDialog
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.input.FvListInput implements FvInputComponent
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _input : JSlider;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** gap value between 2 slider tricks. ( default is 10 )*/
	public static var TRICK_INTERSPACE : Number = 2;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Creates and returns a {@link FvSliderInput} with passed-in {@code slider} 
	 * component.
	 */
	public static function create( slider : JSlider ) : FvListInput
	{
		var o : FvListInput = new FvListInput();
		o._input = slider;
		
		return o;
	}
	
	/**
	 * Constructor.
	 * 
	 * <p>Constructs a default horizontal <strong>JSlider</strong> with :
	 * <ul>
	 *   <li>setMajorTickSpacing( 20 )</li>	 *   <li>setMinorTickSpacing( 1 )</li>	 *   <li>setSnapToTicks( true )</li>	 *   <li>setPaintTrack( true )</li>	 *   <li>setShowValueTip( true )</li>
	 * </ul>
	 * 
	 * @param orientation the slider's orientation to either JSlider.VERTICAL 
	 * or JSlider.HORIZONTAL.
	 * @param rounded If {@code true} use a <strong>DefaultIntegerBoundedRangeModel</strong> 
	 * as slider model.<br />
	 * Otherwise use the default <strong>DefaultBoundedRangeModel</strong> one.
	 */
	public function FvListInput( orientation : Number, rounded : Boolean )
	{
		_input = new JSlider( orientation );
		
		if( rounded ) _input.setModel( new DefaultIntegerBoundedRangeModel() );
		
		_input.setMajorTickSpacing( 20 );
		_input.setMinorTickSpacing( 1 );
		_input.setPaintTrack( true );
		_input.setShowValueTip( true );
	}
	
	/**
	 * Sets the model that handles the slider's four fundamental 
	 * properties: minimum, maximum, value, extent.
	 *  
	 * @param the data model
	 */
	public function setModel( model : BoundedRangeModel ) : Void
	{
		_input.setModel( model );	
	}
	
	/**
	 * Returns slider data model.
	 */
	public function getModel() : BoundedRangeModel
	{
		return _input.getModel();	
	}
	
	/**
     * Set the slider's orientation.
     * 
     * @param orientation the orientation to either JSlider.VERTICAL 
     * or JSlider.HORIZONTAL
	 */
	public function setOrientation( orientation : Number ) : Void
	{
		if( orientation != _input.getOrientation() )
		{
			_input.setOrientation( orientation );
			_updateSliderWidth();
		}
	}
	
	/**
     * Return this slider's orientation.
     * 
     * @return JSlider.VERTICAL or JSlider.HORIZONTAL
     */
	public function getOrientation() : Number
	{
		return _input.getOrientation();	
	}
	
	/**
	 * Sets the slider's minimum property.
	 *  
	 * @param minimum the minimum to set.
	 */
	public function setMinimum( n : Number ) : Void
	{
		_input.setMinimum( n );	
		_updateSliderWidth();
	}
	
	/**
	 * Returns the minimum value supported by the slider.
	 */
	public function getMinimum() : Number
	{
		return _input.getMinimum();	
	}
	
	/**
	 * Sets the slider's maximum property.
	 *  
	 * @param minimum the minimum to set.
	 */
	public function setMaximum( n : Number ) : Void
	{
		_input.setMaximum( n );	
		_updateSliderWidth();
	}
	
	/**
	 * Returns the maximum value supported by the slider.
	 */
	public function getMaximum() : Number
	{
		return _input.getMaximum();	
	}
	
	/** 
	 * Returns JSlider component to add into a 
	 * {@link fvaswing.components.FvInputComponentDialog} dialog. 
	 */
	public function getContainer() : Component
	{
		return _input;
	}
	
	/**
	 * Sets slider value.
	 */
	public function setValue( n : Number ) : Void
	{
		_input.setValue( n );
	}
	
	/**
	 * Returns textfield text.
	 */
	public function getValue()
	{
		return _input.getValue();
	}
	
	/**
	 * Returns {@code true} if input is valid.
	 */
	public function isValid() : Boolean
	{
		return _input.getValue() != undefined;
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _updateSliderWidth() : Void
	{
		var diff : Number = _input.getMaximum() - _input.getMinimum();
		
		if( _input.getOrientation() == JSlider.HORIZONTAL )
			_input.setWidth( TRICK_INTERSPACE * diff );
		else  _input.setHeight( TRICK_INTERSPACE * diff );
		
		if( _input.getParent() != undefined ) _input.getParent().revalidate();
	}	
}